let express = require('express');
let cheerio = require('cheerio');
let superagent = require('superagent');
let mysql = require('mysql');

let connection = mysql.createPool({
  user: 'root',
  password: 'rootroot',
  port: 3306,
  host: 'localhost',
  database: 'inter'
});


superagent.get('https://github.com/haizlin/fe-interview/blob/master/lib/React.md').end((err, res) => {
  let localArr = [];
  if (err) {
    console.log('爬取失败');
  } else {
    // console.log(res.text);// html
    let $ = cheerio.load(res.text);
    // $ 是一个方法
    $('.markdown-body ul li a').each((ind, ele) => {
      let obj = {
        title: $(ele).text(),// 文本
        href: $(ele).attr('href') // 文本
      };
      localArr.push(obj);
      connection.query(`insert into react (type,title,href) values("${'react'}","${obj.title}","${obj.href}")`, (err, res) => {
        if (err) {
          console.log(err);
        } else {
          console.log('插入成功');
        }
      });
    });
    // console.log(localArr);
  }
});



